package org.zjt.spark.book
import org.apache.spark.{SparkConf, SparkContext}

/**
  * 做外链接 left join
  */
object LeftOutTest extends App {
  var sparkConf = new SparkConf().setMaster("local[2]").setAppName("WeatherSort")
  val sc = new SparkContext(sparkConf)
  val product = sc.textFile("D:\\Idea workspace\\scala-demo\\src\\main\\resource\\leftOut-producer.txt").map {
    a => {
      val data = a.split("\t");
      (data(1), data(0) + "," + data(2))
    }
  }

  val user = sc.textFile("D:\\Idea workspace\\scala-demo\\src\\main\\resource\\leftOut-user.txt").map {
    a => {
      val data = a.split("\t");
      (data(2), data(0) + "," + data(1))
    }
  }


  var data = product.leftOuterJoin(user).coalesce(2).map(a => (a._2._1.split(",").head,
    a._2._2 match {
      case Some(a) => a
      case None => ""
    })
  ).reduceByKey(_+" , "+_)

  println(data.collect().mkString("\n"))

  sc.stop()
}
